<?php

vendor("Simvis.InputModule");

class TestAction extends Action {

    public function index() {
        $configs = D("Config");
        $modules = D("Module");
        $tests = D("Test");
        $testitems = $tests->where(1)->select();
        foreach ($testitems as &$testitem) {
            $cfgitem = $configs->find($testitem['config_id']);
            $moduleitem = $modules->find($cfgitem['module_id']);
            $testitem['config'] = $cfgitem;
            $testitem['module'] = $moduleitem;
        }
        $this->items = $testitems;

        $this->content = "Test:manager";
        $this->display("Public:layout");
    }

    public function perform() {
        $config_id = $_REQUEST['config_id'];
        if (empty($config_id)) {
            $_SESSION['message'] = "Illegal invoke";
            $this->redirect("Config/index");
        }
        $tests = D("Test");
        $configs = D("Config");
        $configitem = $configs->find($config_id);
        $modules = D("Module");
        $moduleitem = $modules->find($configitem['module_id']);
        echo "<html><body><pre>";
        echo "Loading module ....\n";
        $im = new InputModule();
        $im->prepare(("Template/{$moduleitem['filename']}"));
        $configset = unserialize(convert_uudecode($configitem['configset']));
        $im->fill($configset);
        echo "Generating config file ....\n";
        $configcontent = $im->generate($configset);
        $file = fopen("/tmp/simu_tmp.conf","w");
        if(!$file){
            echo "Write config file failed!\n";
        }else{
            $cnt = fwrite($file,$configcontent);
            echo $cnt." bytes writed into /tmp/simu_tmp.conf \n";
        }
        flush();
        fclose($file);

        echo "Unzipping attachment file ..." . $configitem['attachment'] . "\n";
        exec("unzip -qo "."Attachment/".$configitem['filename']." -d /tmp 2>&1",$outputarray);
        $output = implode("\n", $outputarray);
        echo $output . "\n";
        $cmdline = $configitem['cmdline'] . " -c/tmp/simu_tmp.conf -d/tmp/simu_tmp -flog " . $configitem['program'] . " <" . $configitem['inputfile']." 2>&1";
        $starttime = date("Y-m-d H:i:s");
        echo "Executing cmdline:" . $cmdline . " \nStart at:" . $starttime . "\n";
        unset($outputarray);
        exec("dir /w C:\\", $outputarray);
        $output = implode("\n", $outputarray);
        echo $output . "\n";
        //Handle report file
        $reportfile = file("/tmp/simu_tmp.log", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        $startline = 0;
        for ($i = 0; $i < count($reportfile); $i++) {
            $line = $reportfile[$i];
            if (strpos($line, "OSSim:beginTime")) {
                $startline = $i;
                break;
            }
        }
        $reportarr = array();
        for ($i = $startline; $i < count($reportfile); $i++) {
            $line = $reportfile[$i];
            $linearr = explode(":", $line);
            $linehead = $linearr[0];
            for ($j = 1; $j < count($linearr); $j++) {
                $linetoken = $linearr[$j];
                $linetokenarr = explode("=", $linetoken);
                $reportarr[$linehead . ":" . $linetokenarr[0]] = $linetokenarr[1];
            }
        }
        $result = convert_uuencode(serialize($reportarr));
        //end of handle report file
        $endtime = date("Y-m-d H:i:s");
        echo "Finished at:" . $endtime . "\n";

        $data = array(
            "config_id" => $config_id,
            "starttime" => $starttime,
            "endtime" => $endtime,
            "result" => $result,
            "output" => $output
        );        
        $res = $tests->data($data)->add();
        echo "Write to database ... No." . $res . "\n";
        echo "</pre></body></pre>";
    }

    public function result() {
        $id = $_REQUEST['id'];
        if (empty($id)) {
            $_SESSION['message'] = "Illegal invoke";
            $this->redirect("Test/index");
        }
        $tests = D("Test");
        $testitem = $tests->find($id);
        if (!empty($testitem)) {
            echo "<pre>" . implode("\n",unserialize(convert_uudecode($testitem['result']))) . "</pre>";
        }
    }

    public function output() {
        $id = $_REQUEST['id'];
        if (empty($id)) {
            $_SESSION['message'] = "Illegal invoke";
            $this->redirect("Test/index");
        }
        $tests = D("Test");
        $testitem = $tests->find($id);
        if (!empty($testitem)) {
            echo "<pre>" . $testitem['output'] . "</pre>";
        }
    }

    public function delete() {
        $id = $_REQUEST['id'];
        if (empty($id)) {
            $_SESSION['message'] = "Illegal invoke";
            $this->redirect("Test/index");
        }
        $tests = D("Test");
        if ($tests->delete($id)) {
            $_SESSION['message'] = "Test deleted!";
        } else {
            $_SESSION['message'] = "Delete failed!";
        }
        $this->redirect("Test/index");
    }

}

?>